package com.zhiyou.app.config.ordertaskhandler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zhiyou.app.pojo.bo.TaskResult;
import com.zhiyou.app.pojo.enums.OrderStatusEnum;
import com.zhiyou.app.pojo.vo.dict.IdColorItem;
import com.zhiyou.app.pojo.vo.dict.IdSizeItem;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhiyou.app.common.constant.CommonConstant.DICT_POOL;

@Component
@Scope("prototype")
@EqualsAndHashCode(callSuper = true)
@Data
public class StIdPhotoTaskHandler extends BaseOrderTaskHandler<StIdPhotoTaskHandler> {
    private String photoUrl;
    private String size;
    private String color;
    private String render = "纯色";
    private Integer customerWidth = 0;
    private Integer customerHeight = 0;


    @Override
    public JSONObject parseWorkflow(JSONObject workFlow) {
        if (workFlow != null) {
            return setValues(workFlow, new String[]{"1.inputs.image",
                            "2.inputs.size",
                            "2.inputs.bgcolor",
                            "2.inputs.render"},
                    new Object[]{getComfyUIService().uploadFile(photoUrl), DICT_POOL.getByClass(size, IdSizeItem.class).getDisplayName(),
                            DICT_POOL.getByClass(color, IdColorItem.class).getDisplayName(), render});
        }
        return null;
    }

    @Override
    public void postInitParam(String param) {
        BeanUtil.copyProperties(JSONUtil.toBean(param, this.getClass(), true), this, "taskService", "comfyUIService");
    }


    @Override
    public TaskResult parseResult(Map<String, List<String>> result, Map<String, Object> config) {
        String stId = "9";
        String stIdLayout = "8";
        String stIdWater = "11";
        String stIdWaterLayout = "10";
        if (result != null && !result.isEmpty()) {
            Map<String, String> res = new HashMap<>();
            //需解密
            if (config.get("orderStatus").equals(OrderStatusEnum.PAID.getCode())) {
                res.put("stId", result.get(stId).get(0));
                res.put("stIdLayout", result.get(stIdLayout).get(0));
            } else {
                res.put("stId", result.get(stIdWater).get(0));
                res.put("stIdLayout", result.get(stIdWaterLayout).get(0));
            }
            return new TaskResult()
                    .setFinished(true)
                    .setTaskType(getTaskType())
                    .setOrderNum(config.getOrDefault("orderNum", "").toString())
                    .setResultMap(res);
        }
        return null;
    }

    @Override
    protected String getTaskType() {
        return "STID";
    }
}